package com.syn.synold4s.vm;

import android.app.Application;
import android.content.Intent;
import androidx.annotation.NonNull;
import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.apiresult.PagedList;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.basebean.BaseQuery;
import com.gaoyi.basemodule.basebean.LoginDto;
import com.gaoyi.basemodule.basebean.ProjectDto;
import com.gaoyi.basemodule.basebean.UserDto;
import com.gaoyi.basemodule.baseconstant.ApiStatus;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.gaoyi.basemodule.basevm.BaseListViewModel;
import com.google.gson.reflect.TypeToken;
import com.syn.synold4s.R;
import com.syn.synold4s.activity.ProjectSelectionActivity;
import com.syn.synold4s.activity.surveillance.SurveillanceActivity;
import com.syn.synold4s.bean.dto.ProjectSelectionDto;
import com.syn.synold4s.repository.ProjectSelectionRepository;
import java.util.List;

public class ProjectSelectionViewModel extends BaseListViewModel<ProjectSelectionDto> {

    private final ProjectSelectionRepository repository;

    public ProjectSelectionViewModel(@NonNull Application application) {
        super(application);
        repository = new ProjectSelectionRepository();
    }

    public void getProject(BaseQuery query) {
        repository.project(query, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<PagedList<ProjectSelectionDto>> apiResultData = (ApiResultData<PagedList<ProjectSelectionDto>>) apiResult;
                mResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                }
                PagedList<ProjectSelectionDto> data = apiResultData.getData();
                if (data == null) {
                    return;
                }
                List<ProjectSelectionDto> projectSelectionDtos = data.getList();
                if (projectSelectionDtos != null && !projectSelectionDtos.isEmpty()) {
                    addListValue(projectSelectionDtos);
                }
            }

            @Override
            public void onFails(Exception e) {
                e.printStackTrace();
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<PagedList<ProjectSelectionDto>>>() {
        }.getType());
    }

    public void getVerifyLogin(String loginToken) {
        if (getView() != null) {
            getView().showLoading();
        }
        repository.verifyLogin(loginToken, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<LoginDto> apiResultData = (ApiResultData<LoginDto>) apiResult;
                LoginDto data = apiResultData.getData();
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(apiResultData.getStatus());
                ToastUtil.showToast(apiResultData.getMessage());
                UserDto userInfo = data.getUserInfo();
//                SPUtil.getInstance().putString(SPName.ACCOUNT, userInfo.getUserName());
//                SPUtil.getInstance().putString(SPName.ID, userInfo.getId());
                int jumpFlag = SPUtil.getInstance().getInt(SPName.JUMP_FLAG, -1);
                if (-1 == jumpFlag) {
                    SPUtil.getInstance().putString(SPName.TOKEN,
                            data.getToken() == null ? "" : data.getToken());
                } else {
                    SPUtil.getInstance().putString(SPName.NEW_TOKEN,
                            data.getToken() == null ? "" : data.getToken());
                }
                SPUtil.getInstance().putString(SPName.REFRESH_TOKEN,
                        data.getRefreshToken() == null ? "" : data.getRefreshToken());
                ProjectDto proj = userInfo.getProj();
                if (proj == null) {
                    SPUtil.getInstance().putString(SPName.PROJ_ID, "");
                    SPUtil.getInstance().putString(SPName.PROJ_CODE, "");
                    SPUtil.getInstance().putString(SPName.PROJ_NAME, "");
                    SPUtil.getInstance().putString(SPName.PROJ_ADDRESS, "");
                } else {
                    SPUtil.getInstance().putString(SPName.PROJ_ID, proj.getId());
                    SPUtil.getInstance().putString(SPName.PROJ_CODE, proj.getCode());
                    SPUtil.getInstance().putString(SPName.PROJ_NAME, proj.getName());
                    SPUtil.getInstance().putString(SPName.PROJ_ADDRESS, proj.getAddress());
                    SPUtil.getInstance().putString(SPName.PROJ_LAT, proj.getLat());
                    SPUtil.getInstance().putString(SPName.PROJ_LNG, proj.getLng());
                }
                Intent intent;
                if (0 == jumpFlag || 1 == jumpFlag) {//loginToken为空；选择项目页再次回到登录页；
                    SPUtil.getInstance().putBoolean(SPName.LOGIN_FLAG, true);
                    SPUtil.getInstance().putBoolean(SPName.TOKEN_FLAG, true);
                    intent = new Intent(mApplication, SurveillanceActivity.class);
                } else {//正常登录流程；进入选择项目页
                    SPUtil.getInstance().putBoolean(SPName.TOKEN_FLAG, false);
                    intent = new Intent(mApplication, ProjectSelectionActivity.class);
                }
//                Intent intent = new Intent(mApplication, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                mApplication.startActivity(intent);
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<LoginDto>>() {
        }.getType());
    }
}
